package top.kpromise.note.modules.user.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.kpromise.common.base.Result;
import top.kpromise.common.utils.SecurityUtils;
import top.kpromise.common.utils.ValidationUtils;
import top.kpromise.note.base.BaseApi;
import top.kpromise.note.modules.user.entity.LoginUser;
import top.kpromise.note.modules.user.entity.UserEntity;
import top.kpromise.note.modules.user.service.UserService;

@RestController
@Api(tags = {"用户账号"})
@RequestMapping("/user")
public class Account extends BaseApi {

    private final UserService userService;

    public Account(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/createAccount", method = RequestMethod.PUT)
    @ApiOperation(value = "创建账号")
    @ApiImplicitParam(name = "loginUser", dataType = "LoginUser",
            paramType = "body", required = true)
    public Result<String> createAccount(@RequestBody LoginUser loginUser) throws Exception {
        ValidationUtils.throwIfValidateFailed(loginUser);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(loginUser.getUserName());
        userEntity.setNickName("十三");
        userEntity.setPassword(SecurityUtils.password(loginUser.getUserName(), loginUser.getPassword()));
        userService.save(userEntity);
        return Result.success("用户创建成功，请登录");
    }
}
